package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.SpendClassify;
import cn.wolfcode.crm.serivce.ISpendClassifyService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("spendClassify")
public class SpendClassifyController {
    @Autowired
    private ISpendClassifyService service;

    @RequestMapping("view")
    public String view(Model model,Long id) throws Exception {
        List<SpendClassify> spendClassifies = service.selectRootClassify();
        Long parent_id =0L;
        //获取父分类id
        for (SpendClassify spendClassify : spendClassifies) {
            parent_id = spendClassify.getId();
        }
        model.addAttribute("spendRootClassify",spendClassifies);
        model.addAttribute("spendChildrenClassify",service.selectChildrenSpendClaaify(parent_id));
        return "spendClassify";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(SpendClassify emp,String text) throws Exception {
        try {
            service.saveOrUpdate(emp);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }


    //添加父分类
    @RequestMapping("saveSpendParent")
    @ResponseBody
    public JSONResult saveSpendParent(String text) throws Exception {
        try {
            service.saveSpendParent(text);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("添加失败!");
        }
    }


    //添加子分类
    @RequestMapping("saveSpendChildren")
    @ResponseBody
    public JSONResult saveSpendChildren(String text,Long parentId) throws Exception {
        try {
            service.saveSpendChildren(text,parentId);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("添加失败!");
        }
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<SpendClassify> selectAll() throws Exception {
        List<SpendClassify> spendClassifys = service.selectAll();
        return spendClassifys;
    }



    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) throws Exception {
        JSONResult jsonResult = new JSONResult();
        try {
            service.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
}
